const request = require("../request.js");

/*
第一个post请求获取到新闻列表和下次请求的时间戳（recommend_time）
第二次请求再次发出，将两次获取的新闻重新整理


article_type: 1
"aid": "1091690",
"title": "鲍威尔应该“少说话，多微笑”",
"is_audio": "0",
"share_url": "https://m.huxiu.com/article/1091690.html",
"is_hot": false,
"video_info": {}, // 此类数据需要删除
*/

module.exports = getNewsListWithApi;

function getNewsListWithApi() {
  let list = [],
    recommend_time,
    method = "post",
    url = "https://api-article.huxiu.com/web/article/articleList";
  return request({
    url,
    method,
    params: { platform: "www" },
  })
    .then((res) => {
      list.push(...res.data.dataList);
      recommend_time = res.data.last_dateline;
      console.log("recommend_time: " + recommend_time);
    })
    .then(() =>
      request({
        url,
        method,
        params: { platform: "www", recommend_time },
      })
    )
    .then((res) => {
      list.push(...res.data.dataList);

      list = list
        .filter((d) => !d.video_info)
        .map((d) => ({
          title: d.title,
          url: `https://www.huxiu.com/article/${d.aid}.html`,
        }))
        .slice(0, 24);

      return list;
    });
}

// getNewsListWithApi().then((list) => console.log(list));
